/*  ----------------------------------------------------------------------------
 *  Arello Mobile
 *  Fast 3d Engine
 *  ----------------------------------------------------------------------------
 * Except where otherwise noted, this work is licensed under
 * http://creativecommons.org/licenses/by/3.0
 */

#ifndef SAVEWAVE_H
#define SAVEWAVE_H

#include "Format.h"
#include <filesystem/File.h>

struct WAV
{
	char  rID[4];
	unsigned rLen;
	char  wID[4];
	char  fID[4];
	unsigned fLen;
	unsigned short wFormatTag;
	unsigned short nChannels;
	unsigned nSamplesPerSec;
	unsigned nAvgBytesPerSec;
	unsigned short nBlockAlign;
	unsigned short nFormatSpecific;
};

void save_wave(const filesystem::String & filename, const Format & fmt, const void * data, int size )
{
	WAV header={
		{'R','I','F','F'},
		0,
		{'W','A','V','E'},
		{'f','m','t',' '}
	};
	header.rLen = sizeof(header) + size;
	header.fLen = 16;
	header.wFormatTag = 1;//WAVE_FORMAT_PCM
	header.nChannels = fmt.get_channels();
	header.nSamplesPerSec = fmt.get_frequency();
	header.nAvgBytesPerSec = fmt.get_frequency() * fmt.get_sample_size();
	header.nBlockAlign = fmt.get_sample_size();
	header.nFormatSpecific = fmt.get_bit_rate();

	filesystem::File fw( filename, filesystem::File::WRITE );
	fw.write( &header, sizeof(header) );
	fw.write( "data", 4 );
	fw.write( &size, 4 );
	fw.write( data, size );
}

#endif SAVEWAVE_H